//
//  GWChatInputView.m
//  CloudDoctorApp
//
//  Created by hushaohui on 16/5/24.
//  Copyright © 2016年 GoWin. All rights reserved.
//

//
//  HUTextView.m
//  YY键盘
//
//  Created by hu on 16/5/27.
//  Copyright © 2016年 hu. All rights reserved.
//


#import "GWChatInputView.h"
#import "HUChatMoreView.h"
#import "HUFaceView.h"
#import "Masonry.h"
#import "YYKeyboardManager.h"
#import "TZImagePickerController.h"
#import "RecordHUD.h"
#import "UIImage+Orientation.h"

@interface GWChatInputView()<YYKeyboardObserver,HPGrowingTextViewDelegate,HUChatMoreViewDelegate,HUChatMoreViewDataSource,HUChatFaceViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate,TZImagePickerControllerDelegate,Mp3RecorderDelegate>
{
    CGFloat _changeFrontHeight;                   ///< 变化前的高度
    CGFloat  _recordTime;                         ///< 录音变化的时间
    BOOL    _isRecordVoice;                       ///< 是否录音
}
@property (strong, nonatomic) Mp3Recorder *MP3;

@property (nonatomic, weak  ) UIView            * superView;/**< 父视图 */
@property (nonatomic, assign) CGRect            superFrame;
@property (strong, nonatomic) UIButton          *voiceButton;/**< 切换录音模式按钮 */
@property (strong, nonatomic) UIButton          *voiceRecordButton;/**< 录音按钮 */
@property (strong, nonatomic) UIButton          *faceButton;/**< 表情按钮 */
@property (strong, nonatomic) UIButton          *moreButton;/**< 更多按钮 */
@property (strong, nonatomic) HPGrowingTextView *textView;/**< 输入框*/
@property (strong, nonatomic) HUFaceView        *faceView;/**< 表情视图*/
@property (nonatomic, strong) HUChatMoreView    *moreView;/**< 更多视图*/

@property (copy, nonatomic  ) NSString          *inputText;
@property (nonatomic, assign) CGRect             lastFrame;

@property (nonatomic, assign) CGRect tmpFrame;


@end
@implementation GWChatInputView

+ (instancetype)shareManager {
    static GWChatInputView* object = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object = [[self allocWithZone:NULL]init];
        object.type = GWKeyboardCaseVideo;
        UIWindow* window = [[[UIApplication sharedApplication]delegate]window];
        object.superFrame = window.frame;
        object.superView = window;
        object.isFirstResponse = NO;
        [KYYKeyBoard addObserver:object];
        [object setup];
    });
    return object;
}
- (void)show {
    UIWindow* window = [[[UIApplication sharedApplication] delegate]window];
    [window makeKeyAndVisible];
    [window addSubview:self];
    self.alpha = 0;
    self.faceView.alpha = 0;
    self.moreView.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
        self.faceView.alpha = 1;
        self.moreView.alpha = 1;
    }];
}

- (void)hide {
    [self endInputing];
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
        self.faceView.alpha = 0;
        self.moreView.alpha = 0;
    }];
}
- (void)dealloc {
    [KYYKeyBoard removeObserver:self];
    [self removeFromSuperview];
    [self.faceView removeFromSuperview];
    [self.moreView removeFromSuperview];
    
}
- (void)endInputing {
    if (self.isFirstResponse) {
        [self showViewWithType:GWFunctionViewShowNothing];
        //结束编辑
    }else if ([self.faceButton isSelected]|| [self.moreButton isSelected]) {
        [self showViewWithType:GWFunctionViewShowNothing];
        
    }

    //放弃第一响应者
    self.isFirstResponse = NO;
    [self.textView endEditing:YES];
}

#pragma mark YYKeyBoard 键盘代理事件
- (void)keyboardChangedWithTransition:(YYKeyboardTransition)transition {
    CGRect kbFrame = [KYYKeyBoard convertRect:transition.toFrame toView:self.superView];
    __weak typeof(self)weakSelf = self;
    //键盘消失
    if (!transition.toVisible) {
        self.isFirstResponse = NO;
        [UIView animateWithDuration:transition.animationDuration delay:0 options:transition.animationOption animations:^{
            if (weakSelf.faceButton.selected || weakSelf.moreButton.selected) {
                return ;
            }
            CGRect textframe = self.frame;
            if (self.voiceButton.selected) {
                textframe.origin.y    = self.superFrame.size.height - KMinHeight;
                textframe.size.height = KMinHeight;
            }else {
                textframe.origin.y = self.superFrame.size.height - self.frame.size.height;
            }
            
            [self setFrame:textframe animated:NO toType:GWFunctionViewShowNothing];
        } completion:nil];
    }
    //键盘出现
    else{
        
        [UIView animateWithDuration:transition.animationDuration delay:0 options:transition.animationOption animations:^{
            CGRect textframe = weakSelf.frame;
            if (_changeFrontHeight) {
                textframe.size.height = _changeFrontHeight;
                _changeFrontHeight = 0;
            }
            textframe.origin.y = SCREEN_HEIGHT - kbFrame.size.height - textframe.size.height ;
            [weakSelf setFrame:textframe animated:NO toType:GWFunctionViewShowKeyboard];
            weakSelf.faceButton.selected = NO;
            weakSelf.moreButton.selected = NO;
        } completion:nil];
    }
}
//初始self的frame
- (void)initTextViewFrameWithtoType:(GWFunctionViewShowType)toType{
    CGRect textframe = self.frame;
    if (self.voiceButton.selected) {
        //        textframe.origin.y    = self.superFrame.size.height - KMinHeight ;
        textframe.origin.y    = SCREEN_HEIGHT - KMinHeight ;
        textframe.size.height = KMinHeight;
    }else {
        textframe.origin.y = SCREEN_HEIGHT - self.frame.size.height;
    }
    if (self.voiceButton.selected){
        toType = GWFunctionViewShowVoice;
    }
    [self setFrame:textframe animated:YES toType:toType];
    
}
- (void)setup {
    [self updateSelfFrame];
    [self addSubview:self.voiceButton];
    [self addSubview:self.faceButton];
    [self addSubview:self.moreButton];
    [self addSubview:self.textView];
    [self addSubview:self.voiceRecordButton];
}
- (void)updateSelfFrame {
    self.backgroundColor = [UIColor whiteColor];
    
    self.frame = CGRectMake(0, SCREEN_Height_HU - KMinHeight , SCREEN_Width_HU, KMinHeight);
    self.tmpFrame = self.frame;
    UIImageView *topLine = [[UIImageView alloc] init];
    topLine.backgroundColor = [UIColor colorWithRed:184/255.0f green:184/255.0f blue:184/255.0f alpha:1.0f];
    [self addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.mas_top);
        make.height.mas_equalTo(@.5f);
    }];
}
#pragma mark 更新UI布局
- (void)updateConstraints{
    [super updateConstraints];
    [self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
        make.bottom.equalTo(self.mas_bottom).with.offset(-6);
        //        make.width.equalTo(self.voiceButton.mas_height);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(32);
    }];

    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.bottom.equalTo(self.mas_bottom).with.offset(-6);
        //        make.width.equalTo(self.moreButton.mas_height);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(32);
    }];
    
    [self.faceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.moreButton.mas_left).with.offset(-10);
        make.bottom.equalTo(self.mas_bottom).with.offset(-6);
        //        make.width.equalTo(self.faceButton.mas_height);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(32);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.voiceButton.mas_right).with.offset(10);
//        make.left.equalTo(self.mas_left).with.offset(10);
        make.right.equalTo(self.faceButton.mas_left).with.offset(-10);
        make.top.equalTo(self.mas_top).with.offset(4);
        make.bottom.equalTo(self.mas_bottom).with.offset(-4);
    }];
    [self.voiceRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.voiceButton.mas_right).with.offset(10);
//        make.left.equalTo(self.mas_left).with.offset(10);
        make.right.equalTo(self.faceButton.mas_left).with.offset(-10);
        make.top.equalTo(self.mas_top).with.offset(4);
        make.bottom.equalTo(self.mas_bottom).with.offset(-4);
    }];
}
#pragma mark button点击事件
- (void)buttonAction:(UIButton *)button{
    self.inputText = self.textView.text;
    GWFunctionViewShowType showType = button.tag;
    
    //更改对应按钮的状态
    if (button == self.faceButton) {
        [self.faceButton setSelected:!self.faceButton.selected];
        [self.moreButton setSelected:NO];
        [self.voiceButton setSelected:NO];
    }else if (button == self.moreButton){
        [self.faceButton setSelected:NO];
        [self.moreButton setSelected:!self.moreButton.selected];
        [self.voiceButton setSelected:NO];
    }else if (button == self.voiceButton){
        if (!button.selected) {
            _changeFrontHeight = self.frame.size.height;
        }
        [self.faceButton setSelected:NO];
        [self.moreButton setSelected:NO];
        [self.voiceButton setSelected:!self.voiceButton.selected];
    }
    
    if (!button.selected) {
        showType = GWFunctionViewShowKeyboard;
        [self.textView becomeFirstResponder];
    }else {
        [self.textView resignFirstResponder];
    }
    [self showViewWithType:showType];
}
- (void)showViewWithType:(GWFunctionViewShowType)showType{
    //显示对应的View
    [self showMoreView:showType  == GWFunctionViewShowMore && self.moreButton.selected];
    [self showVoiceView:showType == GWFunctionViewShowVoice && self.voiceButton.selected];
    [self showFaceView:showType  == GWFunctionViewShowFace && self.faceButton.selected];
    
    self.inputText = self.textView.text;
    
    switch (showType) {
            
        case GWFunctionViewShowNothing:{
            self.isFirstResponse = NO;
            self.showType = GWFunctionViewShowNothing;
            [self.faceButton setSelected:NO];
            [self.moreButton setSelected:NO];
            [self initTextViewFrameWithtoType:GWFunctionViewShowNothing];
            break;
        }
        case GWFunctionViewShowVoice:{
            self.isFirstResponse = NO;
            self.showType = GWFunctionViewShowVoice;
            [self initTextViewFrameWithtoType:GWFunctionViewShowVoice];
            break;
        }
        case GWFunctionViewShowMore:{
            self.isFirstResponse = YES;
            self.showType = GWFunctionViewShowMore;
            break;
        }
        case GWFunctionViewShowFace:{
            self.isFirstResponse = YES;
            self.showType = GWFunctionViewShowFace;
            break;
        }
        case GWFunctionViewShowKeyboard:{
            self.isFirstResponse = YES;
            self.showType = GWFunctionViewShowKeyboard;
            break;
        }
        default:
            break;
    }
    
}
#pragma mark  切换输入状态 输入语音 表情 图片
- (void)showVoiceView:(BOOL)show{
    self.voiceButton.selected = show;
    self.voiceRecordButton.hidden = !show;
}
- (void)showFaceView:(BOOL)show{
    __weak typeof(self)weakSelf = self ;
    
    if (show) {
        //成为第一响应者
        self.isFirstResponse = YES;
        [self.superView addSubview:self.faceView];
        CGRect changeFrame = self.frame;
        changeFrame.origin.y = SCREEN_HEIGHT - _faceView.frame.size.height - changeFrame.size.height;
        [UIView animateWithDuration:0.25 delay:0 options:7 animations:^{
            [weakSelf setFrame:changeFrame animated:NO toType:GWFunctionViewShowFace];
            _faceView.frame = CGRectMake(0, CGRectGetMaxY(weakSelf.frame), SCREEN_Width_HU, _faceView.frame.size.height);
        } completion:nil];
        //正在编辑
        
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            _faceView.frame = CGRectMake(0, self.superFrame.size.height, SCREEN_Width_HU, _faceView.frame.size.height);
        } completion:^(BOOL finished) {
            [weakSelf.faceView removeFromSuperview];
        }];
        //结束编辑
        
    }
}
- (void)showMoreView:(BOOL)show{
    __weak typeof(self)weakSelf = self ;
    
    if (show) {
        //成为第一响应者
        self.isFirstResponse = YES;
        [self.superView addSubview:self.moreView];
        CGRect changeFrame = self.frame;
        changeFrame.origin.y = self.superFrame.size.height - self.moreView.frame.size.height - changeFrame.size.height;
        [UIView animateWithDuration:0.25 delay:0 options:7 animations:^{
            [weakSelf setFrame:changeFrame animated:NO toType:GWFunctionViewShowMore];
            _moreView.frame = CGRectMake(0, CGRectGetMaxY(changeFrame), SCREEN_Height_HU, _moreView.frame.size.height);
        } completion:nil];
        //正在编辑
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            _moreView.frame = CGRectMake(0, self.superFrame.size.height, SCREEN_Height_HU, _moreView.frame.size.height);
        } completion:^(BOOL finished) {
            [weakSelf.moreView removeFromSuperview];
        }];
        //结束编辑
    }
}
//处理编辑状态
/**
 *  将文字中带表情的字符处理换成图片显示
 *
 *  @param text 未处理的文字
 *
 *  @return 处理后的文字
 */
+ (NSMutableAttributedString *)emotionStrWithString:(NSString *)text URLBlock:(URLBlock)URLBlock{
    return [HUFaceManager emotionStrWithString:text URLBlock:^(NSString *content, NSUInteger type) {
        if (URLBlock) URLBlock(content,type);
    }];
}

//开始录音
- (void)startRecordVoice{
    _isRecordVoice = YES;
    [RecordHUD setTitle:@"开始录音"];
    [RecordHUD show];
    [self.MP3 startRecord];
}
//取消录音
- (void)cancelRecordVoice{
    _isRecordVoice = NO;
    [RecordHUD setTitle:@"已取消录音"];
    [self.MP3 cancelRecord];
    [RecordHUD dismiss];
}
//录音完成 发送
- (void)confirmRecordVoice{
    _isRecordVoice = NO;
    [self.MP3 stopRecord];
    [RecordHUD dismiss];
}
//更新录音显示状态,手指向上滑动后提示松开取消录音
- (void)updateCancelRecordVoice{
    _isRecordVoice = NO;
    [RecordHUD setTitle:@"松手取消录音"];
    [RecordHUD setImage:@"mic_cancel"];
}
//更新录音状态,手指重新滑动到范围内,提示向上取消录音
- (void)updateContinueRecordVoice{
    _isRecordVoice = YES;
    [RecordHUD setTitle:@"离开按钮取消录音"];
}
#pragma mark - MP3录音方法
- (void)endConvertWithMP3FileName:(NSString *)fileName {
    if (fileName) {
        [self sendVoiceMessage:fileName seconds:_recordTime];
    }else{
        
    }
}
- (void)beginConvert {
    //开始转换录音文件
}
//录音太短 被取消了
- (void)failRecord{
    [RecordHUD setTitle:@"录音太短,已取消"];
}
- (void)recording:(float)recordTime volume:(float)volume{
    _recordTime = recordTime;
    if (recordTime >= 60) {
        [self.MP3 stopRecord];
    }
    if (_isRecordVoice) {
        [RecordHUD setImage:[NSString stringWithFormat:@"mic_%.0f.png",volume * 30 > 5 ? 5 : volume*30]];
    }
}
- (void)playVoiceFile:(NSData *)voiceData {
    [self.MP3 playVoiceFile:voiceData];
}
//停止播放
- (void)stopPlayVoiceFile {
    [self.MP3.player stop];
}
#pragma mark 播放器delegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    if (self.delegate && [self.delegate respondsToSelector:@selector(audioPlayerDidFinishPlaying:successfully:)]) {
        [self.delegate audioPlayerDidFinishPlaying:player successfully:flag];
    }
}
#pragma mark - HUFaceView 代理方法
- (void)faceViewSendFace:(NSString *)faceName{
    if ([faceName isEqualToString:@"[删除]"]) {
        [self growingTextView:self.textView shouldChangeTextInRange:NSMakeRange(self.textView.text.length - 1, 1) replacementText:@""];
    }else if ([faceName isEqualToString:@"发送"]){
        NSString *text = self.textView.text;
        if (!text || text.length == 0) {
            return;
        }
        [self sendTextMessage:_textView.text];
        self.inputText = @"";
        self.textView.text = @"";
        //结束编辑
        [self endInputing];
        //        [self showViewWithType:GWFunctionViewShowFace];
    }else{
        self.textView.text = [self.textView.text stringByAppendingString:faceName];
    }
}
#pragma mark HUChatMoreView 代理方法
- (NSArray *)titlesOfMoreView:(HUChatMoreView *)moreView{
    NSMutableArray* titleArray = [@[@"拍摄",@"照片"]mutableCopy];
    switch (self.type) {
        case GWKeyboardImageText:{
            break;
        }
        case GWKeyboardCase:{
            [titleArray addObject:@"病例"];
            break;
        }
        case GWKeyboardVideo:{
            [titleArray addObject:@"视频聊天"];
            break;
        }
        case GWKeyboardCaseVideo:{
            [titleArray addObject:@"病例"];
            [titleArray addObject:@"视频聊天"];
            break;
        }
        default:
            break;
            
    }
    return titleArray;
}
- (NSArray *)imageNamesOfMoreView:(HUChatMoreView *)moreView{
    NSMutableArray* imageArray = [@[@"chat_bar_icons_camera",@"chat_bar_icons_pic"] mutableCopy];
    switch (self.type) {
        case GWKeyboardImageText:{
            break;
        }
        case GWKeyboardCase:{
            [imageArray addObject:@"chat_bar_icons_case"];
            break;
        }
        case GWKeyboardVideo:{
            [imageArray addObject:@"chat_bar_icons_video"];
            break;
        }
        case GWKeyboardCaseVideo:{
            [imageArray addObject:@"chat_bar_icons_case"];
            [imageArray addObject:@"chat_bar_icons_video"];
            break;
        }
            
        default :{
            
            break;
        }
    }
    return imageArray;
}
- (void)moreView:(HUChatMoreView *)moreView selectIndex:(HUChatMoreItemType)itemType {
    UIWindow* window = [[[UIApplication sharedApplication]delegate]window];
    switch (itemType) {
        case HUChatMoreItemCamera:{
            //显示拍照
            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"" message:@"您的设备不支持拍照" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                [alertController addAction:action];
                [window.rootViewController presentViewController:alertController animated:YES completion:nil];
                break;
            }
            UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
            pickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
            pickerC.delegate = self;
            [window.rootViewController presentViewController:pickerC animated:YES completion:nil];
            break;
        }
        case HUChatMoreItemAlbum:{
            //显示相册
            TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
            [window.rootViewController presentViewController:imagePickerVc animated:YES completion:nil];
            break;
        }
            //        case HUChatMoreItemLocation: {
            //            //显示地理位置
            //            break;
            //        }
        case HUChatMoreItemCase: {
            if (self.type == GWKeyboardCase || self.type == GWKeyboardCaseVideo) {
                
                //病例
                if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarSendCase)]){
                    [self.delegate chatBarSendCase];
                }
            }else if (self.type == GWKeyboardVideo) {
                //视频聊天
                if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarStartVedio)]){
                    [self.delegate chatBarStartVedio];
                }
                
            }
            break;
        }
        case HUChatMoreItemVideo: {
            //视频聊天
            if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarStartVedio)]){
                [self.delegate chatBarStartVedio];
            }
            break;
        }
            
            
        default:
            break;
    }
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets{
    UIWindow* window = [[[UIApplication sharedApplication]delegate]window];
    
    [self sendImageMessage:photos];
    [window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets infos:(NSArray<NSDictionary *> *)infos{
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIWindow* window = [[[UIApplication sharedApplication]delegate]window];
    
    //判断资源类型
    UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    picture = [picture imageCompressForWidth:picture targetWidth:SCREEN_WIDTH * 2];
    
    
    [self sendImageMessage:@[picture]];
    [window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 输入框代理方法
- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [self sendTextMessage:growingTextView.text];
        return NO;
    }else if (text.length == 0){
        //判断删除的文字是否符合表情文字规则
        NSString *deleteText = [growingTextView.text substringWithRange:range];
        if ([deleteText isEqualToString:@"]"]) {
            NSUInteger location = range.location;
            NSUInteger length = range.length;
            NSString *subText;
            while (YES) {
                if (location == 0) {
                    return YES;
                }
                location -- ;
                length ++ ;
                subText = [growingTextView.text substringWithRange:NSMakeRange(location, length)];
                if (([subText hasPrefix:@"["] && [subText hasSuffix:@"]"])) {
                    break;
                }
            }
            growingTextView.text = [growingTextView.text stringByReplacingCharactersInRange:NSMakeRange(location, length) withString:@""];
            [growingTextView setSelectedRange:NSMakeRange(location, 0)];
            //            [self textViewDidChange:self.textView];
            return NO;
        }
    }
    return YES;
}
/**
 *  发送普通的文本信息,通知代理
 *
 *  @param text 发送的文本信息
 */
- (void)sendTextMessage:(NSString *)text{
    if (!text || text.length == 0) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendMessage:)]) {
        [self.delegate chatBar:self sendMessage:text];
    }
    self.inputText = @"";
    self.textView.text = @"";
    [self setFrame:CGRectMake(0, self.superFrame.size.height - KMinHeight - KYYKeyBoard.keyboardFrame.size.height, self.frame.size.width, KMinHeight) animated:NO toType:GWFunctionViewShowNothing];
    //    [self showViewWithType:GWFunctionViewShowNothing];
    
}

/**
 *  通知代理发送语音信息
 *
 *  @param voiceData 发送的语音信息data
 *  @param seconds   语音时长
 */
- (void)sendVoiceMessage:(NSString *)voiceFileName seconds:(NSTimeInterval)seconds{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendVoice:seconds:)]) {
        [self.delegate chatBar:self sendVoice:voiceFileName seconds:seconds];
    }
}

/**
 *  通知代理发送图片信息
 *
 *  @param image 发送的图片
 */
- (void)sendImageMessage:(NSArray<UIImage *> *)image{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendPictures:)]) {
        [self.delegate chatBar:self sendPictures:image];
    }
}
#pragma mark 键盘随着输入高度增高
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
    self.isFirstResponse = YES;
    static float initValue = 36 ;
    float changeValue = height - initValue;
    if (changeValue > 0) {
        self.isGrow = YES;
    }else{
        self.isGrow = NO;
    }
    if (changeValue != 0) {
        CGRect viewFrame = self.frame;
        viewFrame.origin.y -= changeValue;
        viewFrame.size.height += changeValue;
        //        self.frame = viewFrame;
        //        [self setFrame:viewFrame animated:NO toType:GWFunctionViewShowKeyboard];
        [self setFrame:viewFrame];
        initValue = height;
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarGrowingTextViewEditing:)]){
            [self.delegate chatBarGrowingTextViewEditing:growingTextView];
        }
    }
    
}
- (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView{
    if (!growingTextView.window.isKeyWindow) {
        [growingTextView.window makeKeyAndVisible];
    }
}
#pragma mark SET && GET
- (HPGrowingTextView *)textView{
    if (!_textView) {
        _textView = [[HPGrowingTextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:16.0f];
        _textView.delegate = self;
//        _textView.keyboardType = UIKeyboardTypeDecimalPad;
        _textView.layer.cornerRadius = 4.0f;
        _textView.layer.borderColor = [UIColor colorWithRed:204.0/255.0f green:204.0/255.0f blue:204.0/255.0f alpha:1.0f].CGColor;
        _textView.returnKeyType = UIReturnKeySend;
        _textView.layer.borderWidth = .5f;
        _textView.layer.masksToBounds = YES;
        [_textView.inputView addSubview:self.faceView];
    }
    return _textView;
}
- (UIButton *)voiceButton{
    if (!_voiceButton) {
        _voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _voiceButton.tag = GWFunctionViewShowVoice;
        [_voiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_voice_normal"] forState:UIControlStateNormal];
        [_voiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_input_normal"] forState:UIControlStateSelected];
        [_voiceButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_voiceButton sizeToFit];
        
    }
    return _voiceButton;
}
- (UIButton *)voiceRecordButton{
    if (!_voiceRecordButton) {
        _MP3 = [[Mp3Recorder alloc]initWithDelegate:self];
        _voiceRecordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _voiceRecordButton.hidden = YES;
        //        _voiceRecordButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_voiceRecordButton setBackgroundColor:[UIColor lightGrayColor]];
        _voiceRecordButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [_voiceRecordButton setTitle:@"按住 说话" forState:UIControlStateNormal];
        [_voiceRecordButton setTitle:@"松开 发送" forState:UIControlStateHighlighted];
        
        [_voiceRecordButton addTarget:self action:@selector(startRecordVoice) forControlEvents:UIControlEventTouchDown];
        [_voiceRecordButton addTarget:self action:@selector(cancelRecordVoice) forControlEvents:UIControlEventTouchUpOutside];
        [_voiceRecordButton addTarget:self action:@selector(confirmRecordVoice) forControlEvents:UIControlEventTouchUpInside];
        [_voiceRecordButton addTarget:self action:@selector(updateCancelRecordVoice) forControlEvents:UIControlEventTouchDragExit];
        [_voiceRecordButton addTarget:self action:@selector(updateContinueRecordVoice) forControlEvents:UIControlEventTouchDragInside];
        _voiceRecordButton.layer.cornerRadius = 5;
    }
    return _voiceRecordButton;
}
- (UIButton *)moreButton{
    if (!_moreButton) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.tag = GWFunctionViewShowMore;
        [_moreButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_more_normal"] forState:UIControlStateNormal];
        [_moreButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_more_highlight"] forState:UIControlStateSelected];
        [_moreButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_moreButton sizeToFit];
    }
    return _moreButton;
}
- (UIButton *)faceButton{
    if (!_faceButton) {
        _faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _faceButton.tag = GWFunctionViewShowFace;
        [_faceButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_face_normal"] forState:UIControlStateNormal];
        [_faceButton setBackgroundImage:[UIImage imageNamed:@"chat_bar_input_normal"] forState:UIControlStateSelected];
        [_faceButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_faceButton sizeToFit];
    }
    return _faceButton;
}
- (HUFaceView *)faceView {
    if (!_faceView) {
        _faceView = [[HUFaceView alloc] initWithFrame:CGRectMake(0, self.superFrame.size.height, self.frame.size.width, kFunctionViewHeight)];
        _faceView.delegate = self ;
        _faceView.backgroundColor = self.backgroundColor ;
    }
    return _faceView;
}

- (void)setFrame:(CGRect)frame animated:(BOOL)animated toType:(GWFunctionViewShowType)toType{
    if (animated) {
        [UIView animateWithDuration:.3 animations:^{
            [self setFrame:frame];
        }];
    }else{
        [self setFrame:frame];
    }
    CGFloat changeHeight;
    changeHeight = self.frame.origin.y - self.tmpFrame.origin.y;
    if (changeHeight != 0 || (self.showType != toType)) {
        self.tmpFrame = self.frame;
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarChangeHeight:toType:)]) {
            [self.delegate chatBarChangeHeight:changeHeight toType:toType];
            self.showType = toType;
        }
    }
    if (toType == GWFunctionViewShowKeyboard ||
        toType == GWFunctionViewShowFace ||
        toType == GWFunctionViewShowMore) {
        if (self.isFirstResponse) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarTextViewDidChangeEditing:)]){
                [self.delegate chatBarTextViewDidChangeEditing:self.textView];
                return;
            }
        }else{
            if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarTextViewDidBeginEditing:)]) {
                [self.delegate chatBarTextViewDidBeginEditing:self.textView];
                return;
            }
        }
        
    }else if (toType == GWFunctionViewShowVoice ||
              toType == GWFunctionViewShowNothing){
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarTextViewDidEndEditing:)]) {
            [self.delegate chatBarTextViewDidEndEditing:self.textView];
            return;
        }
    }
}
- (void)setType:(GWKeyboardType)type {
    _type = type;
    CGRect moreFrame = CGRectMake(0, SCREEN_Height_HU, SCREEN_Width_HU, kFunctionViewHeight / 2);
    //这里可以根据type设置moreview的大小
    self.moreView = [[HUChatMoreView alloc] initWithFrame:moreFrame];
    self.moreView.delegate = self;
    self.moreView.dataSource = self;
    self.moreView.backgroundColor = [UIColor whiteColor];
    [self.moreView reloadData];
}
@end
